package weixin

import (
	// "github.com/martini-contrib/render"
	// "log"
	// "github.com/go-martini/martini"
	"crypto/sha1"
	"fmt"
	"net/http"
	"sort"
	"strings"
	// "server/conf"
	"server/lib/Imartini"
)

var (
	token = "negocat"
)

func validate(res http.ResponseWriter, req *http.Request, log *Imartini.Mlog) string {
	signature := req.FormValue("signature")
	timestamp := req.FormValue("timestamp")
	nonce := req.FormValue("nonce")
	echostr := req.FormValue("echostr")

	sign := []string{token, timestamp, nonce}
	sort.Strings(sign)

	b := sha1.Sum([]byte(strings.Join(sign, "")))

	if signature == fmt.Sprintf("%x", b) {
		return echostr
	} else {
		return ""
	}
}
